//
//  ImageUtil.m
//  TableBarControllerDemo
//
//  Created by 边果互动 on 2021/3/23.
//

#import "ImageUtil.h"

@implementation ImageUtil

#pragma mark - SDWebImg加载图片
+ (void) loadImage: (UIImageView *) imageView andUrl: (NSString *) url {
    [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@""]];
}

#pragma mark - 压缩图片到指定大小
+ (NSData *) compressImage:(UIImage *)image {
    NSInteger maxLength = 1024*1024 ;//1024*1024*3
     // Compress by quality
     CGFloat compression = 1;
     NSData *data = UIImageJPEGRepresentation(image, compression);
     if (data.length < maxLength) return data;
     CGFloat max = 1;
     CGFloat min = 0;
     for (int i = 0; i < 6; ++i) {
         compression = (max + min) / 2;
         data = UIImageJPEGRepresentation(image, compression);
         if (data.length < maxLength * 0.9) {
             min = compression;
         } else if (data.length > maxLength) {
             max = compression;
         } else {
             break;
         }
     }
     UIImage *resultImage = [UIImage imageWithData:data];
     if (data.length < maxLength) return data;
     // Compress by size
     NSUInteger lastDataLength = 0;
     while (data.length > maxLength && data.length != lastDataLength) {
         lastDataLength = data.length;
         CGFloat ratio = (CGFloat)maxLength / data.length;
         CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)), (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
         UIGraphicsBeginImageContext(size);
         [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
         resultImage = UIGraphicsGetImageFromCurrentImageContext();
         UIGraphicsEndImageContext();
         data = UIImageJPEGRepresentation(resultImage, compression);
     }
     return data;
}

#pragma mark - 压缩图片到指定质量(兆)
+ (NSData *)compressImage:(UIImage *)image endValue:(NSInteger)value {
    NSInteger maxLength = 1024*1024*value/2 ;//1024*1024*3
    // Compress by quality
    CGFloat compression = 1;
    NSData *data = UIImageJPEGRepresentation(image, compression);
    if (data.length < maxLength) return data;
    CGFloat max = 1;
    CGFloat min = 0;
    for (int i = 0; i < 6; ++i) {
        compression = (max + min) / 2;
        data = UIImageJPEGRepresentation(image, compression);
        if (data.length < maxLength * 0.9) {
            min = compression;
        } else if (data.length > maxLength) {
            max = compression;
        } else {
            break;
        }
    }
    //    NSLog(@"当前选择的图片大小---中间返回的大小%ld",data.length);
    UIImage *resultImage = [UIImage imageWithData:data];
    if (data.length < maxLength) return data;
    // Compress by size
    NSUInteger lastDataLength = 0;
    while (data.length > maxLength && data.length != lastDataLength) {
        lastDataLength = data.length;
        CGFloat ratio = (CGFloat)maxLength / data.length;
        CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)), (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
        UIGraphicsBeginImageContext(size);
        [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
        resultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        data = UIImageJPEGRepresentation(resultImage, compression);
    }
    return data;
}
@end
